import useForm from './useForm';
import FormContext from './useForm/FormContext';
import type { ReactNode } from 'react';
import type { DataProps, FormInstance } from './interface';

interface FormProps {
	onReset?: () => void;
	onFinish?: (data: any) => void;
	onFinishFailed?: (errorInfo: any) => void;
	children?: ReactNode;
	initialValues?: DataProps;
	form?: FormInstance;
	[key: string]: any;
}

const Form = (props: FormProps) => {
	const { children, onFinish, onReset, ...payload } = props;

	const [formRef] = useForm();

	formRef.setConfigWays({ onFinish, onReset });

	return (
		<form
			{...payload}
			onSubmit={(e) => {
				// 阻止默认事件
				e.preventDefault();
				e.stopPropagation();
				formRef.submit();
			}}
			onReset={(e) => {
				e.preventDefault();
				e.stopPropagation();
				formRef.resetFields();
			}}
		>
			<FormContext.Provider value={formRef}>{children}</FormContext.Provider>
		</form>
	);
};

export default Form;
